//
//  LoginScreen.m
//  Gekko
//
//  Created by jose on 02/06/13.
//  Copyright (c) 2013 Jose Luis Lopez. All rights reserved.
//

#import "LoginScreen.h"

@interface LoginScreen ()

@end

@implementation LoginScreen

@synthesize labelSlogan, buttonLogIn, buttonForgot, buttonNoAccount, labelUser, labelPassword, labelLearnMore, textUser, textPassword, isUp;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [labelSlogan setText:NSLocalizedString(@"SLOGAN", nil)];
    [labelSlogan setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [labelUser setText:NSLocalizedString(@"USERNAME", nil)];
    [labelUser setFont:[UIFont fontWithName:kFontBold size:16]];

    [labelPassword setText:NSLocalizedString(@"PASSWORD", nil)];
    [labelPassword setFont:[UIFont fontWithName:kFontBold size:16]];

    [labelLearnMore setText:NSLocalizedString(@"LEARN MORE", nil)];
    [labelLearnMore setFont:[UIFont fontWithName:kFontBold size:12]];
    [labelLearnMore sizeToFit];
    
    UIView *underline = [[UIView alloc] initWithFrame:CGRectMake(labelLearnMore.frame.origin.x,
                                                                  labelLearnMore.frame.origin.y + labelLearnMore.frame.size.height - 4, labelLearnMore.frame.size.width, 1)];
    [underline setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:underline];
    
    UIButton *buttonLearn = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonLearn setFrame:labelLearnMore.frame];
    [buttonLearn addTarget:self action:@selector(learnMorePressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonLearn];
    
    [textUser setFont:[UIFont fontWithName:kFontLight size:16]];

    [textPassword setFont:[UIFont fontWithName:kFontLight size:16]];
    
    [buttonLogIn setTitle:NSLocalizedString(@"LOG IN", nil) forState:UIControlStateNormal];
    [buttonLogIn.titleLabel setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [buttonNoAccount setTitle:NSLocalizedString(@"NO ACCOUNT", nil) forState:UIControlStateNormal];
    [buttonNoAccount.titleLabel setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [buttonForgot setTitle:NSLocalizedString(@"FORGOT", nil) forState:UIControlStateNormal];
    [buttonForgot.titleLabel setFont:[UIFont fontWithName:kFontBold size:16]];
    
    isUp = NO;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    tap.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tap];
}

- (IBAction)loginPressed:(UIButton *)sender {
    
    [[NSUserDefaults standardUserDefaults] setValue:@"2342342342" forKey:kKeyUserId];
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)noAccountPressed:(UIButton *)sender {
    
    [self performSegueWithIdentifier:@"PushToRegister" sender:self];
}

- (IBAction)forgotPressed:(UIButton *)sender {
    
    [self performSegueWithIdentifier:@"PushToReset" sender:self];
}

- (void)learnMorePressed:(UIButton *)sender {

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kURLGetGekko]];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    
    if ([[UIScreen mainScreen] bounds].size.height < 650 && !isUp) {
        
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.1];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y - 88);
        isUp = YES;
        
        [UIView commitAnimations];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    if ([textField isEqual:textUser]) {
        
        [textPassword becomeFirstResponder];
    } else {
        
        if ([[UIScreen mainScreen] bounds].size.height < 650 && isUp) {
            

            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.1];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
            
            self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 88);
            isUp = NO;
            
            [UIView commitAnimations];
        }
    }
    
    return YES;
}

-(void)hideKeyboard {
    
    NSLog(@"Tapped");
    [textPassword resignFirstResponder];
    [textUser resignFirstResponder];
    if ([[UIScreen mainScreen] bounds].size.height < 650 && isUp) {
        
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.1];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 88);
        isUp = NO;
        
        [UIView commitAnimations];
    }
}

- (void)viewDidUnload {
    [self setLabelSlogan:nil];
    [super viewDidUnload];
}
@end
